/**
 * @file {cf_pol目标库flaw表model}
 */
 'use strict'

 const { DataTypes, Model, Op } = require('../../../azeroth').sequelize;
 const {jobInitData} = require('../common/init_data');
 class JobPol extends Model { }
 
 /**
  * @desc 映射需要的字段 【源库的 部分字段 --> 目标库字段】
  */
  const initJobData = (sequelize_aim) => {
    JobPol.init(
         ...jobInitData,
         {
             freezeTableName: true,
             sequelize: sequelize_aim,
             modelName: 'job',
             timestamps: false
         }
     )
  }
  /**
   * 获取源库的最新上传uid
   * @param {*} sequelize_source 
   * @returns 
   */
  JobPol.getMxUidfromSourceJob = async (sequelize_source) => {
    let sql = 'select max(uid) as uid from job';
    let res = await sequelize_source.query(sql, { type: sequelize_source.QueryTypes.SELECT });
    return res[0].uid;
  }
  /**
   * 获取目标库的同步位置
   * @param {*} sequelize_aim 
   * @returns 
   */
  JobPol.getLatestJobId = async (sequelize_aim) => {
    let sql = 'select max(uid) as uid from job';
    let res = await sequelize_aim.query(sql, { type: sequelize_aim.QueryTypes.SELECT });
    return res[0].uid;
  }
  /**
   * get job info by panel_id
   * @param {Sequelize} sequelize_source 
   * @param {String} uid 
   * @returns 
   */
  JobPol.getJobInfoByPanelId = async (sequelize_source, queryStr) => {
    let query = typeof(queryStr) == 'string' ? `(select a.job_id from sheet a inner join pol_aoi b on a.uid = b.sheet_id where b.panel_id = "${queryStr}")` : `${queryStr}`;

    let sql = 'select uid, job_name, sheet_sum as panel_sum, ok_number, ng_number, start_time, start_time_str, finish_time, finish_time_str ' +
    'from job where uid = ' + query;
    let res = await sequelize_source.query(sql, { type: sequelize_source.QueryTypes.SELECT });
    return res[0];
  }
  /**
   * 上传job信息
   * @param {Array} data 上传的缺陷信息数组
   * @returns
   */
  JobPol.uploadPolInfo = async (data) => {
    let res = await JobPol.bulkCreate([data], {
      updateOnDuplicate: ['uid', 'job_name', ' panel_sum', ' ok_number', 'ng_number', 'start_time', 'finish_time', 'start_time_str', 'finish_time_str'],
    });
    return res;
  }
 module.exports = {
     JobPol,
     initJobData
 };